#! /usr/bin/sdlbrt

finp$="AntarcticAdventureTune_WaldteufelWaltz.tic"

function writehex(v1,q1)
  if q1=0 then:
    v2=int(v1/16)
  else:
    v2=v1 mod 16
    end if
  v3=48+v2
  if v2>9 then
    v3=v3+7
    end if
  return v3
  end function

dim bline[16],bnam$[16]
for i=0 to 15:bline[i]=32:next
bline[0x01]=4:bline[0x02]=4:bline[0x0C]=3:bline[0x0D]=3:bline[0x0A]=16:bline[0x04]=240
bnam$[00]="unknown":bnam$[01]="sprites_bg":bnam$[02]="sprites_fg":bnam$[03]="cover"
bnam$[04]="map":bnam$[05]="code":bnam$[06]="unknown":bnam$[07]="unknown"
bnam$[08]="unknown":bnam$[09]="sfx":bnam$[10]="waveform":bnam$[11]="unknown"
bnam$[12]="palette":bnam$[13]="tunes_notes":bnam$[14]="tunes_frames":bnam$[15]="unknown"

open finp$ for input as #1
while eof(1)=0:
  qs=readbyte(1)
  s0=readbyte(1)
  s1=readbyte(1)
  s2=readbyte(1)
  st=(s2*65536)+(s1*256)+s0

  suf$=".hex":if qs=5 then:suf$=".lua":end if

  open finp$+"_"+str$(qs)+"_"+bnam$[qs]+suf$ for output as #2
  for qq=0 to st-1
    rb=readbyte(1)
    if qs=5 then
      writebyte(2,rb)
    else
      writebyte(2,writehex(rb,0))
      writebyte(2,writehex(rb,1))
      if (qq mod bline[qs])=bline[qs]-1 then:writebyte(2,0x0A):end if
      if ((qs=1) or (qs=2)) and ((qq mod 32)=31) then:writebyte(2,0x0A):end if
      end if
    next

  close #2
  wend
close #1



